#include "click-button.h"

ClickButton::ClickButton(QString normal_image_pathname, QString pressed_image_pathname, QString sound_pathname)
{
    if (normal_image_pathname == nullptr)
    {
        qDebug() << "normal_image_pathname = nullptr";
        return;
    }

    if (pressed_image_pathname == nullptr)
    {
        qDebug() << "pressed_image_pathname = nullptr";
        return;
    }

    this->normal_image_pathname = normal_image_pathname;
    this->pressed_image_pathname = pressed_image_pathname;

    this->setButtonImage(normal_image_pathname);

    this->sound = new QSound(sound_pathname, this);
}

ClickButton::~ClickButton()
{
    delete this->sound;
}

void ClickButton::mousePressEvent(QMouseEvent *event)
{
    // 播放点击音效
    this->sound->play();

    this->setButtonImage(pressed_image_pathname);

    return QPushButton::mousePressEvent(event);
}

void ClickButton::mouseReleaseEvent(QMouseEvent *event)
{
    this->setButtonImage(normal_image_pathname);

    return QPushButton::mouseReleaseEvent(event);
}

void ClickButton::setButtonImage(QString image_pathname)
{
    QPixmap pixmap;
    bool result = pixmap.load(image_pathname);

    if(!result) {
        qDebug() << "加载图片 " << image_pathname << " 失败!";
        return;
    }

    // 设置图片缩放比
    pixmap = pixmap.scaled(pixmap.width() * 1, pixmap.height() * 1);

    // 设置图片固定大小
    this->setFixedSize(pixmap.size());

    // 设置不规则图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    this->setIcon(pixmap);

    // 设置图标大小
    this->setIconSize(pixmap.size());
}
